/***************************************************************************************
 The copyright in this software is being made available under the BSD License, included
 below. This software may be subject to other third party rights,including patent rights,
 and no such rights are granted under this license.

 Regents of the Tsinghua University, Peking University, Zhejiang University, HKUST
 Tsinghua University, Peking University, Zhejiang University, HKUST 2011

 Copyright (c) 2011, Regents of the Tsinghua University, Peking University, Zhejiang
 University, HKUST. All Rights Reserved.

 Redistribution and use in source and binary forms, with or without modification, are
 permitted provided that the following conditions are met:
   * Redistributions of source code must retain the above copyright notice,
     this list of conditions and the following disclaimer.
   * Redistributions in binary form must reproduce the above copyright notice,
     this list of conditions and the following disclaimer in the documentation
     and/or other materials provided with the distribution.
   * Neither the names of the copyright holders nor the names of its
     contributors may be used to endorse or promote products derived from this
     software without specific prior written permission.

 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
 OF SUCH DAMAGE.
***************************************************************************************/

/*
*************************************************************************************
* File name: defines.h
* Function: Headerfile containing some useful global definitions
*
*************************************************************************************
*/

#ifndef _DEFINES_H_
#define _DEFINES_H_

#include <stdlib.h>

#if defined(WIN32) && !defined(__MINGW32__)
#pragma warning(disable: 4996)
#pragma warning(disable: 4244)

# define  snprintf _snprintf
# define  open     _open
# define  close    _close
# define  read     _read
# define  write    _write
# define  lseek    _lseeki64
# define  fsync    _commit
# define  tell     _telli64
# define  ftime     _ftime
# define  TIMEB    _timeb
# define  TIME_T    LARGE_INTEGER
# define  OPENFLAGS_WRITE _O_WRONLY|_O_CREAT|_O_BINARY|_O_TRUNC

#define _S_IREAD        0000400 /* read permission, owner */
#define _S_IWRITE       0000200 /* write permission, owner */

# define  OPEN_PERMISSIONS _S_IREAD | _S_IWRITE
# define  OPENFLAGS_READ  _O_RDONLY|_O_BINARY
# define  inline   _inline
# define  forceinline __forceinline
#else
# include <unistd.h>
# include <sys/time.h>

# include <time.h>
# include <stdint.h>
#if defined(OPENMP)
# include <omp.h>
#endif

# define  TIMEB    timeb
# define  TIME_T   struct timeval
# define  tell(fd) lseek(fd, 0, SEEK_CUR)
# define  OPENFLAGS_WRITE O_WRONLY|O_CREAT|O_TRUNC
# define  OPENFLAGS_READ  O_RDONLY
# define  OPEN_PERMISSIONS S_IRUSR | S_IWUSR
//# define  O_BINARY       0x8000

# if __STDC_VERSION__ >= 199901L
/* "inline" is a keyword */
# else
#  define inline /* nothing */
# endif
# define  forceinline inline

#define min(a,b) (((a) < (b)) ? (a) : (b))

#define max(a,b) (((a) > (b)) ? (a) : (b))

#endif

#define TOTRUN_NUM    15


#define TRACE           0       //!< 0:Trace off 1:Trace on

#define OPT_HP

#define QUARTER_PIXEL    1
#define QUARTER_wangIF   1

#if QUARTER_PIXEL
#define ROUNDING(x)             ((x > 0 ? x+1 : x-1)/4)
#endif


#define RFM_MIN(a,b)  (((a)<(b)) ? (a) : (b))
#define RFM_MAX(a,b)  (((a)<(b)) ? (b) : (a))
#define RFM_ABS(a)              ((a)<(0)   ? (-(a)):(a))
#define RFM_CLIP1(a)      ((a)>255 ? 255 : ((a)<0  ?  0:(a)))
#define RFM_CLIP3(a,b,c)  ((a)<(b) ? (b) : ((a)>(c)?(c):(a)) )
#define MEDIAN(a,b,c)     (a + b + c - RFM_MIN(a, RFM_MIN(b, c)) - RFM_MAX(a, RFM_MAX(b, c)))

#define RFM_MAX_VALUE       999999

#define MIN_QP          0
#define MAX_QP          63
#define QUANT_PERIOD    8

#define IBLOCK  11

#define IS_INTRA(MB)    ((MB)->mb_type==I8MB)
#define IS_INTER(MB)    ((MB)->mb_type!=I8MB)
#define IS_INTERMV(MB)  ((MB)->mb_type!=I8MB  && (MB)->mb_type!=SKIP)
#define IS_DIRECT(MB)   ((MB)->mb_type==0     && (img->   type==    B_IMG ))
#define IS_COPY(MB)     ((MB)->mb_type==SKIP    && (img->   type!=I_IMG))
#define IS_P8x8(MB)     ((MB)->mb_type==INTER8x8)

// Quantization parameter range
#define MIN_QP           0
#define MAX_QP          63

#define B4_SIZE          4
#define B8_SIZE          8
#define MB_BLOCK_SIZE   16

#define INTRA_MODE_ADAPTIVE         1	//luma enable:1  disable:0
#define INTRA_MODE_ADAPTIVE_CHROMA  1	//chroma 
#define INTRA_LEFT                  0
#define INTRA_UP                    1
#define INTRA_NO                    2
#define UV                          2
#define UV_MODE              INTER8x8

#define NO_INTRA_PMODE  5       //!< #intra prediction modes
#define EOS             1       //!< End Of Sequence
#define SOP             2       //!< Start Of Picture
#define SOS             3       //!< Start Of Slice

#define DECODING_OK     0
#define SEARCH_SYNC     1

#define MVPRED_MEDIAN   0
#define MVPRED_L        1
#define MVPRED_U        2
#define MVPRED_UR       3

#define MAX_CODED_FRAME_SIZE 1500000
#define SVA_STREAM_BUF_SIZE 1024

#define MAX_VALUE              (1<<30)

#define MAXREFNUM        2
#define MAXREFNUM_PLUS_1 3
#define MAX_REF_NUM        2
#define MAX_REF_NUM_PLUS_1 3

#define SYNTAX_REPORT   0 // add by Xingguo Zhu
#define REC_YUV         0 // add by Xingguo Zhu

#define DEBLOCK                 0

#endif
